﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Fluent;
using System.Data;
using ReklamaWPF.Grids;
using System.Windows.Forms;



namespace ReklamaWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
 
    public partial class MainWindow : Window
    {
        AdvertGrid advGrid;
        BlocksGrid blockGrid;
        Pivot1Grid pv1Grid;
        Pivot2Grid pv2Grid;
        TableGrid tblGrid;

        private static string advid = "0";
        private static string advidx = "0";
        private static string searchwordAdv = "";

         private static string blkid = "0";
         private static string blkidx = "0";
         private static string searchwordBlock = "";



        public MainWindow()
        {
            InitializeComponent();

            DataBase db = new DataBase("192.168.100.100\\SQLEXPRESS", "sa", "tornado", "PASystem");
            DataTable tb = db.getTable("select * from Anonsai an left join AnonsaiClips anc on an.ClipId=anc.id");

            advGrid = (AdvertGrid)System.Windows.Application.LoadComponent(new Uri("ReklamaWPF;component\\Grids\\AdvertGrid.xaml", System.UriKind.RelativeOrAbsolute));
            blockGrid = (BlocksGrid)System.Windows.Application.LoadComponent(new Uri("ReklamaWPF;component\\Grids\\BlocksGrid.xaml", System.UriKind.RelativeOrAbsolute));
            pv1Grid = (Pivot1Grid)System.Windows.Application.LoadComponent(new Uri("ReklamaWPF;component\\Grids\\Pivot1Grid.xaml", System.UriKind.RelativeOrAbsolute));
            pv2Grid = (Pivot2Grid)System.Windows.Application.LoadComponent(new Uri("ReklamaWPF;component\\Grids\\Pivot2Grid.xaml", System.UriKind.RelativeOrAbsolute));
            tblGrid = (TableGrid)System.Windows.Application.LoadComponent(new Uri("ReklamaWPF;component\\Grids\\TableGrid.xaml", System.UriKind.RelativeOrAbsolute));
           // gridControl1.DataSource = tb;
            // AdvertiseGridControl.View.ShowGroupPanel = false;
            
            

           // pivotGridControl1.DataSource = tb;
        }

    

             
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Calendar_AddAppointment(object sender, RoutedEventArgs e)
        {

        }

        private void enableGrid(String name)
        {
            if (name.Equals("AdvertGrid"))
            {
                //if (this.FindName("PivoteTable")!=null)
                mainPanel.Children.Remove(blockGrid);
                mainPanel.Children.Remove(pv1Grid);
                mainPanel.Children.Remove(pv2Grid);
                mainPanel.Children.Remove(tblGrid);
                // if (this.FindName("SimpleTable") == null)
                try{
                mainPanel.Children.Add(advGrid);
                }
                catch (Exception ex) { }
            }
            if (name.Equals("BlocksGrid"))
            {
                //if (this.FindName("SimpleTable") != null)
                mainPanel.Children.Remove(advGrid);
                mainPanel.Children.Remove(pv1Grid);
                mainPanel.Children.Remove(pv2Grid);
                mainPanel.Children.Remove(tblGrid);
                // if (this.FindName("PivotTable") == null)
                try{
                mainPanel.Children.Add(blockGrid);
                }
                catch (Exception ex) { }
            }
            if (name.Equals("Pivot1Grid"))
            {
                //if (this.FindName("SimpleTable") != null)
                mainPanel.Children.Remove(advGrid);
                mainPanel.Children.Remove(blockGrid);
                mainPanel.Children.Remove(pv2Grid);
                mainPanel.Children.Remove(tblGrid);
                       // if (this.FindName("PivotTable") == null)
                try
                {
                    mainPanel.Children.Add(pv1Grid);
                }
                catch (Exception ex) { }
            }
            if (name.Equals("Pivot2Grid"))
            {
                //if (this.FindName("SimpleTable") != null)
                mainPanel.Children.Remove(advGrid);
                mainPanel.Children.Remove(blockGrid);
                mainPanel.Children.Remove(pv1Grid);
                mainPanel.Children.Remove(tblGrid);
                // if (this.FindName("PivotTable") == null)
                try{
                mainPanel.Children.Add(pv2Grid);
                }
                catch (Exception ex) { }
            }
            if (name.Equals("TableGrid"))
            {
                //if (this.FindName("PivoteTable")!=null)
                mainPanel.Children.Remove(advGrid);
                mainPanel.Children.Remove(blockGrid);
                mainPanel.Children.Remove(pv1Grid);
                mainPanel.Children.Remove(pv2Grid);
                // if (this.FindName("SimpleTable") == null)
                try
                {
                    mainPanel.Children.Add(tblGrid);
                }
                catch (Exception ex) { }
            }
            // UserControl uc = (UserControl)System.Windows.Application.LoadComponent(new Uri("ReklamaWPF;component\\Grids\\" + name + ".xaml", System.UriKind.RelativeOrAbsolute));

        }


        private void Ribbon_SelectedTabChanged(object sender, SelectionChangedEventArgs e)
        {
            int ix = ((RibbonTabControl)e.Source).SelectedIndex;
            if (ix >= 0)
            {
                switch (ix)
                {
                    case 0: 
                        //AdvertiseGridControl.Visibility = Visibility.Visible;
                        //blocksGridControl.Visibility = Visibility.Hidden;
                        //pivotGridControl1.Visibility = Visibility.Hidden;
                        //pivotGridControl2.Visibility = Visibility.Hidden;
                        enableGrid("AdvertGrid");
                        break;
                    case 1:// AdvertiseGridControl.Visibility = Visibility.Hidden;
                        //blocksGridControl.Visibility = Visibility.Visible;
                        //pivotGridControl1.Visibility = Visibility.Hidden;
                        //pivotGridControl2.Visibility = Visibility.Hidden;
                        enableGrid("BlocksGrid");
                        break;
                    case 2: //AdvertiseGridControl.Visibility = Visibility.Hidden;
                        //blocksGridControl.Visibility = Visibility.Hidden;
                        //pivotGridControl1.Visibility = Visibility.Visible;
                        //pivotGridControl2.Visibility = Visibility.Hidden;
                        enableGrid("Pivot1Grid");
                        break;
                    case 3: //AdvertiseGridControl.Visibility = Visibility.Hidden;
                        //blocksGridControl.Visibility = Visibility.Hidden;
                        //pivotGridControl1.Visibility = Visibility.Hidden;
                        //pivotGridControl2.Visibility = Visibility.Visible;
                        enableGrid("Pivot2Grid");
                        break;
                    case 4: //AdvertiseGridControl.Visibility = Visibility.Hidden;
                        //blocksGridControl.Visibility = Visibility.Hidden;
                        //pivotGridControl1.Visibility = Visibility.Hidden;
                        //pivotGridControl2.Visibility = Visibility.Hidden;
                        enableGrid("TableGrid");
                        break;
                }
            }
        }

        private void advNewButton_Click(object sender, RoutedEventArgs e)
        {
            AdvertisingAdd iw = new AdvertisingAdd();
            iw.datePicker1.DisplayDate = DateTime.Now;
            iw.datePicker1.Text = DateTime.Now.ToShortDateString();
            if (iw.ShowDialog() == true)
            {
                //Functionality.SearchInDBAdv(dataGrid1, iw.textBox2.Text);
                advid = "0";
                advGrid.LoadDataByDate(iw.datePicker1.DisplayDate);
            }
        }

        private void advEditButton_Click(object sender, RoutedEventArgs e)
        {
            advGrid.editRecord(searchwordAdv);
        }

        private void blockNewButton_Click(object sender, RoutedEventArgs e)
        {
            BlockAdd iw = new BlockAdd();
            if (iw.ShowDialog() == true)
            {
                //Functionality.SearchInDBBlock(dataGrid1, iw.textBox2.Text);
                blkid = "0";
                blockGrid.LoadDataByDate(iw.datePicker1.DisplayDate);
            }
        }

        private void blockEditButton_Click(object sender, RoutedEventArgs e)
        {

            blockGrid.editRecord(searchwordAdv);
            if (!blkid.Equals("0"))
            {

                BlockAdd iw = new BlockAdd();
                iw.red = true;
                //Functionality.updateBlockIntoDB(iw, dataGrid1, searchword, id);
                iw.red = false;
            }
        }

        private void advSearchButton_Click(object sender, RoutedEventArgs e)
        {
            SearchWindow sw = new SearchWindow();
            if (sw.ShowDialog() == true)
            {
                searchwordAdv = sw.textBox1.Text;
                DataTable tb = Functionality.db.FindStringInTableAdv(searchwordAdv);
                advGrid.LoadData(tb);
            }
        }

        private void timeExportButton_Click(object sender, RoutedEventArgs e)
        {
            pv1Grid.ExportData();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            advGrid.archiveRecord();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {

            string sMessageBoxText = "Ar tikrai norite ištrinti šį įrašą?";
            string sCaption = "Patvirtinimas";

            MessageBoxButton btnMessageBox = MessageBoxButton.YesNo;
            MessageBoxImage icnMessageBox = MessageBoxImage.Warning;

            MessageBoxResult rsltMessageBox = System.Windows.MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);
            if (rsltMessageBox == MessageBoxResult.Yes)
                advGrid.deleteRecord();
        }

        private void advEditArchButton_Click(object sender, RoutedEventArgs e)
        {
            advGrid.editArchivedRecord(searchwordAdv);
        }

        private void anDerleteArchButton_Click(object sender, RoutedEventArgs e)
        {
            advGrid.deleteArchRecord();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            advGrid.restoreArchiveRecord();
        }

        private void blockDerleteButton_Click(object sender, RoutedEventArgs e)
        {
            blockGrid.deleteRecord();
        }


        private void buttonGen_Click(object sender, RoutedEventArgs e)
        {
            tblGrid.LoadData(pv2Grid.pivotGridControl2);
            ribbonTabs.SelectedTabIndex = 4;
        }


        private void tableExpButton_Click(object sender, RoutedEventArgs e)
        {
            tblGrid.ExportData();
        }


    }

    public class TimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                long ticks = long.Parse(value.ToString());
                TimeSpan tm = new TimeSpan(ticks);
                return string.Format("{0:c}", tm);
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return DateTime.Parse(value.ToString());
            }
            else
            {
                return DateTime.Now.Date;
            }
        }
    }

    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) { return ""; }
            else if (value.ToString().Contains("-"))
            {
                return value.ToString();
            }
            else if (value.ToString().Contains("Grand"))
            {
                return "Suma:";
            }
            else
            {
                return value.ToString().Substring(0, 10);
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return DateTime.Parse(value.ToString());
            }
            else
            {
                return DateTime.Now.Date;
            }
        }
    }

    public class SelectedDateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return DateTime.Now.Year + "." + DateTime.Now.Month;
            }
            else
            {
                DateTime dt = DateTime.Parse(value.ToString());
                return dt.Year + "." + dt.Month;
            }
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return DateTime.Parse(value.ToString()+".01");
            }
            else
            {
                return DateTime.Now.Date;
            }
        }
    }

    
}
